var config = require('../../../../utils/config');
var API_URL = config.API_URL;
var util = require('../../../../utils/util');
var account,typ,key;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    code_disabled: false,
    code_time: '获取验证码',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    account = options.account;
    typ = options.typ;
    this.setData({
      account:account,
      typ:typ
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },
  send: function (e) {
    var url;
    var that = this;
    that.setData({
      code_disabled:true
    })
    if (typ == 'email') {
      url = API_URL.sendEmail;
    } else {
      url = API_URL.sendSms;
    }
    var params = {
      mobile: account,
      email: account,
      type: 'unbind'
    }
    util.PRL(url, params)
      .then(res => {
        if (res.data.status == 200) {
            console.log(res)
          key = res.data.data.key;
          util.countdown(that, 120);
        } else {
          that.setData({
            code_disabled: false
          })
          util.showNotice(res.data.msg)
        }
      }).catch(res => {
        that.setData({
          code_disabled: false
        })
        console.log(e);
      })
  },
  unbind: function (e) {
    var code = e.detail.value.code;
    if (code == '') {
      util.showNotice('请输入验证码');
      return
    }
    var params = {
      account: account,
      type: typ,
      key: key,
      code: code
    }
    util.PRL(API_URL.unbindAccount, params)
      .then(res => {
        if (res.data.status == 200) {
          my.showToast({
            content: '解绑成功',
            duration: 800,
          })
          setTimeout(function () {
            my.navigateBack({
              delta: 1
            })
          }, 800)
        } else {
          util.showNotice(res.data.msg)
        }
      }).catch(res => {
        console.log(res)
      })
  }
})